<template>
  <el-dialog :title="!dataForm.id ? '新增生产试验汇总表' : '修改生产试验汇总表'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px">
      <el-form-item label="项目编号" prop="projectCode">
        <el-select v-model="dataForm.projectCode" placeholder="请选择项目编号" class="label_len" :disabled="selectDis">
          <el-option v-for="item in projectList" :key="item.prProjectNumber" :label="item.prProjectNumber+item.prProjectName" :value="item.prProjectNumber"
            ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="检测时间" prop="timeRange">
        <el-date-picker
            v-model="dataForm.timeRange"
            type="daterange"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            placeholder="选择时间范围"
            :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="耕宽 (m)" prop="ploughWide">
        <el-input v-model="dataForm.ploughWide" placeholder="耕宽 " type="number"></el-input>
      </el-form-item>
      <el-form-item label="班次时间 (h)" prop="sailingsTime">
        <el-input v-model="dataForm.sailingsTime" placeholder="班次时间 " type="number"></el-input>
      </el-form-item>
      <el-form-item label="纯工作时间 (h)" prop="jobTime">
        <el-input v-model="dataForm.jobTime" placeholder="纯工作时间 " type="number" @input="calculateEffective(),calculatePureWorkingHour()"></el-input>
      </el-form-item>
      <el-form-item label="故障排除时间 (h)" prop="faultTime">
        <el-input v-model="dataForm.faultTime" placeholder="故障排除时间 " type="number" @input="calculateEffective()"></el-input>
      </el-form-item>
      <el-form-item label="总作业量 (h/m²)" prop="workNumber">
        <el-input v-model="dataForm.workNumber" placeholder="总作业量 " type="number" @input="calculatePureWorkingHour()"></el-input>
      </el-form-item>
      <el-form-item label="故障情况原因" prop="cause">
        <el-input type="textarea" v-model="dataForm.cause" placeholder="故障情况原因"></el-input>
      </el-form-item>
      <el-form-item label="作业质量" prop="workQuality">
        <el-input type="textarea" v-model="dataForm.workQuality" placeholder="作业质量"></el-input>
      </el-form-item>
      <el-row style="margin-top: 20px">
        <el-col :span="12">
          <el-form-item label="有效度计算值">
            <el-input v-model="dataForm.computeEffective" :disabled="true" placeholder="计算有效度" type="number"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="有效度校验值">
            <el-input v-model="dataForm.effective" placeholder="请输入有效度校验值" type="number"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row style="margin-top: 20px">
        <el-col :span="12">
          <el-form-item label="生产率计算值">
            <el-input v-model="dataForm.computePureWorkingHour" :disabled="true" placeholder="纯工作小时生产率" type="number"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="生产率校验值">
            <el-input v-model="dataForm.pureWorkingHour" placeholder="请输入纯工作小时生产率" type="number"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        projectCode: '',
        ploughWide: '',
        sailingsTime: '',
        jobTime: '',
        faultTime: '',
        workNumber: '',
        effective: '',
        pureWorkingHour: '',
        workQuality: '',
        aggregators: '',
        checkers: '',
        cause: '',
        startTime: '',
        endTime: '',
        timeRange: [],
        computeEffective:'',
        computePureWorkingHour:''
      },
      selectDis:false,
      projectList:[],
      dataRule: {
        projectCode: [
          { required: true, message: '项目编号不能为空', trigger: 'blur' }
        ],
        ploughWide: [
          { required: true, message: '耕宽 不能为空', trigger: 'blur',pattern: /^\d+(\.\d+)?$/,}
        ],
        sailingsTime: [
          { required: true, message: '班次时间 不能为空', trigger: 'blur',pattern: /^\d+(\.\d+)?$/,}
        ],
        jobTime: [
          { required: true, message: '纯工作时间 不能为空', trigger: 'blur' ,pattern: /^\d+(\.\d+)?$/,}
        ],
        faultTime: [
          { required: true, message: '故障排除时间 不能为空', trigger: 'blur',pattern: /^\d+(\.\d+)?$/, }
        ],
        workNumber: [
          { required: true, message: '总作业量 不能为空', trigger: 'blur',pattern: /^\d+(\.\d+)?$/, }
        ],
        effective: [
          { required: true, message: '有效度不能为空', trigger: 'blur' }
        ],
        pureWorkingHour: [
          { required: true, message: '纯工作小时生产率不能为空', trigger: 'blur' }
        ],
        aggregators: [
          { required: true, message: '汇总人不能为空', trigger: 'blur' }
        ],
        checkers: [
          { required: true, message: '校核人不能为空', trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: '开始时间不能为空', trigger: 'blur' }
        ],
        endTime: [
          { required: true, message: '结束时间不能为空', trigger: 'blur' }
        ],
        timeRange:[
            { required: true, message: '检测时间不能为空', trigger: 'blur'}
            ]
      }
    }
  },
  mounted (){
    this.getProjectNumber()
  },
  methods: {
    // 计算有效度 jobTime/(jobTime+faultTime) * 100
    calculateEffective(){
      const jobTime = parseFloat(this.dataForm.jobTime) || 0;
      const faultTime = parseFloat(this.dataForm.faultTime) || 0;

      const avg = jobTime / (jobTime + faultTime) * 100;
      this.dataForm.computeEffective = avg.toFixed(0)
      this.dataForm.effective = avg.toFixed(0)
    },
    //计算生产率 workNumber/ jobTime
    calculatePureWorkingHour(){
      const jobTime = parseFloat(this.dataForm.jobTime) || 0;
      const workNumber = parseFloat(this.dataForm.workNumber) || 0;

      const avg = workNumber / jobTime;
      this.dataForm.computePureWorkingHour = avg.toFixed(2);
      this.dataForm.pureWorkingHour = avg.toFixed(2)
    },
    //获取所有项目
    getProjectNumber(){
      this.$http({
            url: this.$http.adornUrl(`/reportapproval/product/getProjectNumber`),
            method: 'get',
            data: this.$http.adornData({})
          }).then(( data ) => {
            if (data && data.code === 0) {
              this.projectList=data.productList
            } else {
              this.$message.error('错误')
            }
          })
    },
    init(id) {
      this.dataForm.id = id || 0
      this.selectDis = false
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/reportapproval/testssummarytable/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(( data ) => {
            if (data && data.code === 0) {
              this.dataForm.projectCode = data.testsSummaryTable.projectCode
              this.dataForm.ploughWide = data.testsSummaryTable.ploughWide
              this.dataForm.sailingsTime = data.testsSummaryTable.sailingsTime
              this.dataForm.jobTime = data.testsSummaryTable.jobTime
              this.dataForm.faultTime = data.testsSummaryTable.faultTime
              this.dataForm.workNumber = data.testsSummaryTable.workNumber
              this.dataForm.effective = data.testsSummaryTable.effective
              this.dataForm.pureWorkingHour = data.testsSummaryTable.pureWorkingHour
              this.dataForm.workQuality = data.testsSummaryTable.workQuality
              this.dataForm.aggregators = data.testsSummaryTable.aggregators
              this.dataForm.checkers = data.testsSummaryTable.checkers
              this.dataForm.cause = data.testsSummaryTable.cause
              this.dataForm.startTime = data.testsSummaryTable.startTime
              this.dataForm.endTime = data.testsSummaryTable.endTime
              this.dataForm.timeRange = [data.testsSummaryTable.startTime,data.testsSummaryTable.endTime]
              this.selectDis = true
              this.dataForm.computeEffective = ''
              this.dataForm.computePureWorkingHour = ''
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.dataForm.startTime = this.dataForm.timeRange[0];
      this.dataForm.endTime = this.dataForm.timeRange[1]
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/reportapproval/testssummarytable/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'projectCode': this.dataForm.projectCode,
              'ploughWide': this.dataForm.ploughWide,
              'sailingsTime': this.dataForm.sailingsTime,
              'jobTime': this.dataForm.jobTime,
              'faultTime': this.dataForm.faultTime,
              'workNumber': this.dataForm.workNumber,
              'effective': this.dataForm.effective,
              'pureWorkingHour': this.dataForm.pureWorkingHour,
              'workQuality': this.dataForm.workQuality,
              'aggregators': this.dataForm.aggregators,
              'checkers': this.dataForm.checkers,
              'cause': this.dataForm.cause,
              'startTime': this.dataForm.startTime,
              'endTime': this.dataForm.endTime
            })
          }).then(( data ) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.warning(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
